<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class TestMysql extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:mysql';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $table = 'alice_activity_shares';
        $sql = "SELECT  `openid`,`big_channel`,`small_channel`,`add_at`,`unionid` FROM `{$table}` group by `openid` having count(`id`) >=3";
        $datas = DB::select($sql);
        foreach ($datas as $data) {
            for($i=0;$i<3;$i++) {
                DB::table('alice_coupons_logs')->insert(['big_channel'=>$data->big_channel,'openid'=>$this->getRandomStr(),'small_channel'=>$data->small_channel,'add_at'=>$data->add_at,'inviter_openid'=>$data->openid]);
            }
        }
        dd(1234);
//        $name = $this->ask('你叫什么名字');
//        $city = $this->choice('你来自哪个城市', [
//            '北京', '杭州', '深圳'
//        ], 0);
//        $password = $this->secret('输入密码才能执行此命令');
//        if ($password != '123') {
//            $this->error('密码错误');
//            exit(-1);
//        }
//        if ($this->confirm('确定要执行此命令吗?')) {
//            $this->info('欢迎来自' . $city . '的' . $name . '访问 Laravel 学院');
//        } else {
//            exit(0);
//        }
//        dd(123);
//        DB::transaction(function() {
//            DB::statement('create table `order_goods_bak2`  select * from `order_goods`');
//            DB::table('order_goods')->truncate();
//        });

        DB::transaction(function() {
            DB::statement('create table `assembles_20190731`  select * from `assembles`');
            DB::table('assembles')->truncate();
        });
        DB::transaction(function() {
            DB::statement('create table `assemble_groups_20190731`  select * from `assemble_groups`');
            DB::table('assemble_groups')->truncate();
        });
        DB::transaction(function() {
            DB::statement('create table `assemble_share_records_20190731`  select * from `assemble_share_records`');
            DB::table('assemble_share_records')->truncate();
        });
        DB::transaction(function() {
            DB::statement('create table `shopping_orders_20190731`  select * from `shopping_orders`');
            DB::table('shopping_orders')->truncate();
        });
//        DB::transaction(function() {
////            Schema::rename('guide', 'catalog_bak');
////            Schema::rename('catalog_baks', 'catalogs');
////            Schema::rename('catalog_temp', 'catalog_baks');
////        });
    }

    public function getRandomStr()
    {

        $str = "";
        $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $max = strlen($str_pol) - 1;
        for ($i = 0; $i < 16; $i++) {
            $str .= $str_pol[mt_rand(0, $max)];
        }
        return $str;
    }
}
